//format transaction data
//load in raw data
//generate new vars

use "${rawdata}2014/transactions.dta", clear
//count
//Number of unique values of application_number is 9204051
//Number of records is  275606097

//amendments are different from appeals; appeals go to patent judges (alleged unfairness on the part of examiner) 
//while amendments go to examiner

gen docketed1 = 0
replace docketed1=1 if event_code=="DOCK"

gen initial_rejection1 = 0
replace initial_rejection1=1 if event_code=="CTNF"

gen initial_amendment1 = 0
replace initial_amendment1=1 if event_code=="A..." 

gen final_rejection1 = 0
replace final_rejection1=1 if event_code=="CTFR"

gen final_amendment1 = 0
replace final_amendment1=1 if event_code=="A.NE" | event_code=="RCEX"

gen appeal_filed1 = 0
replace appeal_filed1=1 if event_code=="N/AP" | event_code=="AP.B" 

gen patent_issued1 = 0
replace patent_issued1=1 if event_code=="PTAC" | event_code=="PGM" | event_code=="PGM/" | event_code=="PILS" | event_code=="MN/=." | event_code=="N/=." | event_code=="EX.R" 

gen patent_abandoned1 = 0
replace patent_abandoned1=1 if event_code=="ABN2" 

///NEED TO BE MORE SOPHISTICATED ABOUT TIMING FOR SOME AMENDMENTS (A.I. : A.LA : A.NQ)
//if they occur between IR and FR, then it’s an initial amendment
//if it occurs after FR, then it’s a final amendment

gen sequence_initial_rej=.
replace sequence_initial_rej=sequence_number if event_code=="CTNF"
bysort application_number: egen min_seq_initial=min(sequence_initial_rej)

gen sequence_final_rej=.
replace sequence_final_rej=sequence_number if event_code=="CTFR"
bysort application_number: egen min_seq_final=min(sequence_final_rej)
//replace min_seq_final=. if min_seq_final==0

gen sequence_A_I=0
replace sequence_A_I=sequence_number if event_code=="A.I."
bysort application_number: replace initial_amendment1=1 if (sequence_A_I>min_seq_initial) & (sequence_A_I<min_seq_final)
bysort application_number: replace final_amendment1=1 if sequence_A_I>min_seq_final

gen sequence_A_LA=0
replace sequence_A_LA=sequence_number if event_code=="A.LA"
bysort application_number: replace initial_amendment1=1 if (sequence_A_LA>min_seq_initial) & (sequence_A_LA<min_seq_final)
bysort application_number: replace final_amendment1=1 if sequence_A_LA>min_seq_final

gen sequence_A_NQ=0
replace sequence_A_NQ=sequence_number if event_code=="A.NQ"
bysort application_number: replace initial_amendment1=1 if (sequence_A_NQ>min_seq_initial) & (sequence_A_NQ<min_seq_final)
bysort application_number: replace final_amendment1=1 if sequence_A_NQ>min_seq_final

sort application_number
by application_number: egen docketed = max(docketed1)
by application_number: egen initial_rejection = max(initial_rejection1)
by application_number: egen initial_amendment = max(initial_amendment1)
by application_number: egen final_rejection = max(final_rejection1)
by application_number: egen final_amendment = max(final_amendment1)
by application_number: egen patent_issued = max(patent_issued1)
by application_number: egen patent_abandoned = max(patent_abandoned1)

//count actions
by application_number: egen suminitial_rejection = sum(initial_rejection1)
by application_number: egen suminitial_amendment = sum(initial_amendment1)
by application_number: egen sumfinal_rejection = sum(final_rejection1)
by application_number: egen sumfinal_amendment = sum(final_amendment1)

save "${filedata}transactions_all_indicators", replace
bysort application_number: gen count=_n
drop if count != 1
drop initial_rejection1 docketed1 initial_amendment1 final_rejection1 final_amendment1 appeal_filed1 patent_issued1 patent_abandoned1 count
save "${filedata}transactions_application_indicators.dta", replace

